package com.dajiba.demo;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.BlurMaskFilter;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.Shader;
import android.os.Handler;
import android.util.AttributeSet;
import android.widget.Scroller;

import androidx.annotation.Nullable;

import com.blankj.utilcode.util.LogUtils;

public class CustomDrawShadow extends BaseCustomView {

    private Matrix matrix;
    private Bitmap bitmap;

    public CustomDrawShadow(Context context) {
        super(context);

        bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.aaaaaaa);

        matrix = new Matrix();

        float[] src = {100, 300,                                    // 左上
                300, 200,                          // 右上
                300, 400,        // 右下
                100, 400};                        // 左下

        float[] dst = {100, 300,                                    // 左上
                300, 400,                        // 右上
                300, 400 - 200,  // 右下
                0, 300};                        // 左下

        // 核心要点
        matrix.setPolyToPoly(src, 0, dst, 0, src.length >> 1); // src.length >> 1 为位移运算 相当于处以2

        // 此处为了更好的显示对图片进行了等比缩放和平移(图片本身有点大)
        matrix.postScale(0.26f, 0.26f);
        matrix.postTranslate(0,200);
    }

    public CustomDrawShadow(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public CustomDrawShadow(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        setLayerType(LAYER_TYPE_SOFTWARE, null);
        scroller = new Scroller(context);



    }


    Handler handler = new Handler();

    Scroller scroller;

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
//        mPaint.setMaskFilter(new BlurMaskFilter(50,BlurMaskFilter.Blur.SOLID));
        //阴影
        mPaint.setShadowLayer(20, 0, 20, getContext().getResources().getColor(R.color.colorPrimary));
        Shader shader = new Shader();
        shader.setLocalMatrix(matrix);
        mPaint.setShader(shader);
        canvas.drawRect(100, 200, 300, 400, mPaint);

        mPaint.setStrokeWidth(5);
        canvas.drawLine(500, 600, 700, 800, mPaint);

//        canvas.drawBitmap(bitmap,200,300,mPaint);

        mPaint.setTextSize(50);
        mPaint.setTextSkewX(0.5f);
        canvas.drawText("哈哈哈哈哈哈",300,800,mPaint);
    }
}
